export default {
  state: {
    goodsList: [],
    selectedAll: false,
    goodsNum: 0,
    priceInfo: {
      payPrice: 0,
      totalPrice: 0,
      accountPrice: 0
    }
  },

  getters: {
    goodsList: state => state.goodsList,
    selectedAll: state => state.selectedAll,
    goodsNum: state => state.goodsNum,
    priceInfo: state => state.priceInfo
  },

  actions: {
    /**
     * 获取购物车商品数量
     * @param { Object } 当前存储数据的state
     */
    getGoodsList ({ state }) {
      // 设置商品列表
      state.goodsList = [
        { id: 1, checked: false, name: '男式精梳棉V领短袖T恤衫', sku: ['白色', 'S(165/88A)'], amount: 1, floorPrice: 3190, originPrice: 4900, tag: '特价', summary: 3190, coverImg: 'http://yanxuan.nosdn.127.net/c5a979a9080fed79748fe455622fcd62.png?quality=95&thumbnail=200x200&imageView' },
        { id: 2, checked: false, name: '女式精梳棉A领短袖保罗衫', sku: ['红色', 'S(165/88A)'], amount: 3, floorPrice: 6990, originPrice: 8000, tag: '折扣', summary: 6990, coverImg: 'http://yanxuan.nosdn.127.net/c5a979a9080fed79748fe455622fcd62.png?quality=95&thumbnail=200x200&imageView' }
      ]
      // 设置购物车内商品总数量
      state.goodsNum = state.goodsList.length
    },

    /**
     * 设置全选所有商品或全不选
     * @param { Object } 当前存储数据的state
     */
    setSelectedAllGoods ({ state }) {
      state.selectedAll = !state.selectedAll
      state.goodsList.forEach(goods => {
        goods.checked = state.selectedAll
      })
      // 重新计算总价
      this.dispatch('calcTotalAmout')
    },

    /**
     * 设置选中商品
     * @param { Object } 当前存储数据的state
     * @param { Object } 当前值
     */
    setSelectedGoods ({ state }, payload) {
      // 初始化成false
      state.selectedAll = true
      // 设置选中装填
      state.goodsList.forEach(goods => {
        // 设置当前商品的选中状态
        if (goods.id === payload.id) {
          goods.checked = !goods.checked
        }
        // 如果当前没有选中，则不选中
        if (!goods.checked) {
          state.selectedAll = false
        }
      })
      // 重新计算总价
      this.dispatch('calcTotalAmout')
    },

    /**
     * 计算总价格和优惠价格
     * @param { Object } 当前存储数据的state
     */
    calcTotalAmout ({ state }) {
      // 初始化价格
      state.priceInfo = {
        payPrice: 0,
        totalPrice: 0,
        accountPrice: 0
      }
      // 取出已经选中的价格信息
      state.goodsList.forEach(goods => {
        // 如果商品是选中的
        if (goods.checked) {
          state.priceInfo.payPrice += goods.floorPrice * goods.amount
          state.priceInfo.totalPrice += goods.originPrice * goods.amount
          state.priceInfo.accountPrice += (goods.originPrice - goods.floorPrice) * goods.amount
        }
      })
    },

    /**
     * 设置选中商品
     * @param { Object } state 当前存储数据的state
     * @param { Object } payload 当前商品
     */
    deleteGoods ({ state }, payload) {
      // 取出当前商品索引
      state.goodsList = state.goodsList.filter(goods => goods.id !== payload.id)
      // 设置购物车内商品总数量
      state.goodsNum = state.goodsList.length
      // 重新计算总价
      this.dispatch('calcTotalAmout')
    },

    /**
     * 删除选中的商品
     * @param { Object } state 当前存储数据的state
     */
    deleteSelectedGoods ({ state }) {
      // 取出当前商品索引
      state.goodsList = state.goodsList.filter(goods => !goods.checked)
      // 设置购物车内商品总数量
      state.goodsNum = state.goodsList.length
      // 重新计算总价
      this.dispatch('calcTotalAmout')
    },

    /**
     * 设置商品数量
     * @param { Object } 当前存储数据的state
     * @param { Object } 当前值
     */
    setGoodsNumber ({ state }, payload) {
      state.goodsList.forEach(goods => {
        if (goods.id === payload.goods.id) {
          goods.amount = payload.num
        }
      })
      // 重新计算总价
      this.dispatch('calcTotalAmout')
    }
  }
}
